﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using TfsGiggle.Core.Parsers;

namespace TfsGiggle.Core.IntegrationTest
{
    class MockItem : IItem
    {
        int _id;
        string _contents;
        string _name;
        IParser _parser;

        internal MockItem(int id, string name, string contents)
        {
            _id = id;
            _name = name;
            _contents = contents;
        }

        public void Parse()
        {
            _parser = ParserFactoryManager.GetParser(_name);
            if (_parser != null)
            {
                _parser.Parse(_name, new StreamReader(DownloadFile()).ReadToEnd());
            }
        }
        
        public int ChangesetId
        {
            get { return _id; }
        }

        public Stream DownloadFile()
        {
            // MemoryStream does not seem to work well with Lucene
            var name = Path.GetTempFileName();
            File.WriteAllText(name, _contents, UTF8Encoding.UTF8);
            return new StreamReader(name, UTF8Encoding.UTF8, true).BaseStream;
        }

        public int Encoding
        {
            get { return UTF32Encoding.UTF8.WindowsCodePage; }
        }

        public string ServerItem
        {
            get { return _name; }
        }

        public DateTime CheckinDate
        {
            get { return DateTime.Now; }
        }

        public long ContentLength
        {
            get { return _contents.Length; }
        }

        public IVersionControlServer VersionControlServer
        {
            get { return MockVersionControlServer.Instance; }
        }

        /// <summary>
        /// Usings
        /// </summary>
        public List<string> UsingDeclarations
        {
            get
            {
                return _parser == null ? null : _parser.UsingDeclarations;
            }
        }

        /// <summary>
        /// Classes
        /// </summary>
        public List<string> ClassDeclarations
        {
            get
            {
                return _parser == null ? null : _parser.ClassDeclarations;
            }
        }

        /// <summary>
        /// Interfaces
        /// </summary>
        public List<string> InterfaceDeclarations
        {
            get
            {
                return _parser == null ? null : _parser.InterfaceDeclarations;
            }
        }

        /// <summary>
        /// Methods
        /// </summary>
        public List<string> MethodDeclarations
        {
            get
            {
                return _parser == null ? null : _parser.MethodDeclarations;
            }
        }
    }
}